package com.example.zhangxin.comevedio.activity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import static android.content.ContentValues.TAG;

/**
 * Created by ZhangXin on 2016/9/13.
 */

public class HomeView extends HorizontalScrollView{
    private LinearLayout mMapper;
    private ViewGroup mMeum;
    private ViewGroup mContent;
    private WindowManager service;
    private int widthPixels;
    private int mMenuRightPadding;
    private int mMeumWidth;
    private int mContentWidth;

    public HomeView(Context context) {
        super(context);
    }

    public HomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        service = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        service.getDefaultDisplay().getMetrics(metrics);
        widthPixels = metrics.widthPixels;
        mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mMapper = (LinearLayout) getChildAt(0);
        mMeum = (ViewGroup) mMapper.getChildAt(0);
        mContent = (ViewGroup) mMapper.getChildAt(1);
        mMeumWidth = mMeum.getLayoutParams().width = widthPixels -mMenuRightPadding;
        mContentWidth = mContent.getLayoutParams().width = widthPixels;

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if(changed){
            this.scrollTo(mMeumWidth,0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_UP:
                int scaleX = getScrollX();

                if(scaleX>=widthPixels/2){

                    this.smoothScrollTo(mMeumWidth,0);
                }else{
                    this.smoothScrollTo(0,0);
                }
                return true;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l*1.0f/mMeumWidth;
        Log.i(TAG, scale+"onScrollChanged: "+l);
        float rigthScale = 0.7f+0.3f*scale;
        float leftScale = 1.0f-0.3f*scale;
        float leftAlpha = 0.6f+0.4f*(1-scale);
        float rigthAlpha = 1f-0.6f*(1-scale);
        mMeum.setScaleX(leftScale);
        mMeum.setScaleY(leftScale);
        mMeum.setAlpha(leftAlpha);
        mContent.setScaleY(rigthScale);
        mContent.setScaleX(rigthScale);
        mContent.setPivotX(0);
        mContent.setPivotY(mContent.getMeasuredHeight()/2);
        mMeum.setTranslationX(scale);
        mContent.setAlpha(rigthAlpha);
    }
    private onToggeChangeListener mOnToggeChangeListener;
    public void setOnToggeChangeListener(onToggeChangeListener tc){
        mOnToggeChangeListener = tc;
    }
    public interface onToggeChangeListener{
        void click();

    }

    public void doSome(){
        if(mOnToggeChangeListener!=null){
            mOnToggeChangeListener.click();
        }
    }

}
